www.gusucode.com > VC++网络版的打字软件源程序-源码程序 > VC++网络版的打字软件源程序-源码程序\code\TypeSrv V2.0\ClientSocket.cpp

    // ClientSocket.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "TypeSrv.h"
#include "ClientSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "Msg.h"
#include "ListenSocket.h"

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{
	m_pListenSocket=NULL;

	m_pFile=NULL;

	m_pArchiveOut=NULL;
	m_pArchiveIn=NULL;

	m_strQuestion=_T("");
	m_strQuestionInfo=_T("");
	m_lTestTime=0;
	m_nInputLanguage=0;

	m_strHostName=_T("");
	m_strID=_T("");
	m_strName=_T("");

	m_nRateOfRight=0;
	m_nSpeedOfType=0;
}

CClientSocket::~CClientSocket()
{
	if(m_pArchiveOut!=NULL)
	{
		delete m_pArchiveOut;
	}

	if(m_pArchiveIn!=NULL)
	{
		delete m_pArchiveIn;
	}

	if(m_pFile!=NULL)
	{
		delete m_pFile;
	}
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::Init()
{
	m_pFile=new CSocketFile(this);

	m_pArchiveIn=new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut=new CArchive(m_pFile,CArchive::store);
}

void CClientSocket::SendSrvClose()
{
	CMsg msg;
	msg.Init();
	msg.nFlag=SF_CLOSE;
	SendMsg(&msg);
}

void CClientSocket::SendLoginSuccess()
{
	CMsg msg;
	msg.Init();
	msg.nFlag=SF_LOGINSUCCESS;
	SendMsg(&msg);
}

void CClientSocket::SendLoginFaild()
{
	CMsg msg;
	msg.Init();
	msg.nFlag=SF_LOGINFAILD;
	SendMsg(&msg);
}

void CClientSocket::SendQuestion(LPCTSTR lpszQuestion, LPCTSTR lpszQuestionInfo, long lTestTime, int nInputLanguage)
{
	CMsg msg;
	msg.Init();
	msg.nFlag=SF_SENDQUESTION;
	msg.m_strQuestion=lpszQuestion;
	msg.m_strQuestionInfo=lpszQuestionInfo;
	msg.m_lTestTime=lTestTime;
	msg.m_nInputLanguage=nInputLanguage;
	SendMsg(&msg);
}


void CClientSocket::Abort()
{
	if(m_pArchiveOut!=NULL)
	{
		m_pArchiveOut->Abort();
		delete m_pArchiveOut;
		m_pArchiveOut=NULL;
	}

}

void CClientSocket::SendMsg(CMsg *pMsg)
{
	TRY
	{
		//发送信息
		if(m_pArchiveOut!=NULL)
		{
			pMsg->Serialize(*m_pArchiveOut);
			m_pArchiveOut->Flush();
		}
	}
	CATCH(CFileException,e)
	{
		Abort();
		AfxMessageBox("发送信息错误!");
	}
	END_CATCH


}

void CClientSocket::ReceiveMsg(CMsg *pMsg)
{
	TRY
	{
		//接收送信息
		pMsg->Serialize(*m_pArchiveIn);
	}
	CATCH(CFileException,e)
	{
		Abort();
		AfxMessageBox("接收信息错误!");
	}
	END_CATCH

}

void CClientSocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	CSocket::OnReceive(nErrorCode);

	do
	{
		CMsg msg;
		ReceiveMsg(&msg);
		
		switch(msg.nFlag)
		{
		case CF_CLOSE://关闭用户
			ReceiveClientClose();//删除用户
			return;//退出
		case CF_USERLOGIN://用户登陆
			ReceiveUserInfo(msg.m_strHostName,msg.m_strID,msg.m_strName);
			break;
		case CF_SENDSCORE://接收用户发送的成绩
			ReceiveTypeScore(msg.m_nRateOfRight,msg.m_nSpeedOfType);
			break;
		case CF_SAVESCORE://客户机保存成绩
			ReceiveSaveScore(msg.m_nRateOfRight,msg.m_nSpeedOfType);
			break;
		case CF_USERLOGOUT://客户机退出考试系统
			ReceiveUserLogout();
			break;
		}
	}
	while(!m_pArchiveIn->IsBufferEmpty());
}


void CClientSocket::ReceiveTypeScore(int nRate, int nSpeed)
{
	m_nRateOfRight=nRate;
	m_nSpeedOfType=nSpeed;
	m_pListenSocket->ReceiveTypeScore(m_strID,m_strName,nRate,nSpeed);
}


void CClientSocket::ReceiveUserInfo(CString strHostName,CString strID, CString strName)
{
	m_strHostName=strHostName;//记录用户机名称

	if(m_pListenSocket->CheckUserInfo(strID)==FALSE)
	{
		SendLoginFaild();//验证失败
		return;
	}

	SendLoginSuccess();//验证成功
	m_strID=strID;
	m_strName=strName;

	//接收用户信息
	m_pListenSocket->ReceiveUserInfo(strHostName,strID,strName);
	
	//发送试题
	SendQuestion(m_strQuestion,m_strQuestionInfo,m_lTestTime,m_nInputLanguage);
}


void CClientSocket::ReceiveClientClose()
{
	m_pListenSocket->ReceiveClientClose(this);
}



void CClientSocket::Close()
{
	SendSrvClose();//发送服务器关闭信息
	CSocket::Close();
}

void CClientSocket::ReceiveSaveScore(int nRate, int nSpeed)
{
	m_pListenSocket->ReceiveSaveScore(m_strID,m_strName,nRate,nSpeed);
}

void CClientSocket::ReceiveUserLogout()
{
	m_pListenSocket->ReceiveUserLogout(m_strID,m_strName);
}